<?php
/**
 * @author Minh Luan
 * @copyright 2012
 */
include_once("ConnectionManager.php");

//Get root category (no parent)
function getAllParentCategory(){
    $query = "select CAT_ID, CAT_NAME_EN, CAT_NAME_VN, CAT_DESCRIPTION_EN, CAT_DESCRIPTION_VN, CAT_PARENT_ID, CAT_IMG_URL 
    from GIAAN_CATEGORY
    where CAT_PARENT_ID = 0";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();    
}

//Get children category of category
function getChildrenCategoryOf($cate_parent_id, $startRecord, $numberOfRecord){
	$query = "select c.CAT_ID, c.CAT_NAME_EN, c.CAT_NAME_VN, c.CAT_PARENT_ID, c.CAT_DESCRIPTION_EN, c.CAT_DESCRIPTION_VN, c.CAT_IMG_URL, 
    (select cc.CAT_NAME_EN from GIAAN_CATEGORY cc where cc.CAT_ID = c.CAT_PARENT_ID) as parent_name_en,
    (select cc.CAT_NAME_VN from GIAAN_CATEGORY cc where cc.CAT_ID = c.CAT_PARENT_ID) as parent_name_vn
    from GIAAN_CATEGORY c 
    where CAT_PARENT_ID = $cate_parent_id 
    order by CAT_NAME_VN
	limit $startRecord, $numberOfRecord";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
	return $result;
}

function getAllChildrenCategoryOf($cate_parent_id){
	$query = "select c.CAT_ID, c.CAT_NAME_EN, c.CAT_NAME_VN, c.CAT_PARENT_ID, c.CAT_DESCRIPTION_EN, c.CAT_DESCRIPTION_VN, c.CAT_IMG_URL,
    (select cc.CAT_NAME_EN from GIAAN_CATEGORY cc where cc.CAT_ID = c.CAT_PARENT_ID) as parent_name_en,
    (select cc.CAT_NAME_VN from GIAAN_CATEGORY cc where cc.CAT_ID = c.CAT_PARENT_ID) as parent_name_vn
    from GIAAN_CATEGORY c 
    where CAT_PARENT_ID = $cate_parent_id 
    order by CAT_NAME_VN";
	ConnectionManager::getInstance()->setQuery($query);
	$result = ConnectionManager::getInstance()->query();
	return $result;
}

function getCategory($cat_id){
    $query = "select CAT_ID, CAT_NAME_EN, CAT_NAME_VN, CAT_DESCRIPTION_EN, CAT_DESCRIPTION_VN, CAT_PARENT_ID, CAT_IMG_URL 
    from GIAAN_CATEGORY
    where CAT_ID = $cat_id";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query(); 
}

function addCategory($cat_name_en, $cat_name_vn, $cat_description_en, $cat_description_vn, $cat_parent_id){
    $query = "insert into GIAAN_CATEGORY(CAT_NAME_EN, CAT_NAME_VN, CAT_DESCRIPTION_EN, CAT_DESCRIPTION_VN, CAT_PARENT_ID, CAT_IMG_URL) 
    values('$cat_name_en', '$cat_name_vn', '$cat_description_en', '$cat_description_vn', $cat_parent_id, '')";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
}

function updateCategoryImageUrl($cat_id, $url){
    $query = "update GIAAN_CATEGORY set CAT_IMG_URL='$url' where CAT_ID = $cat_id";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
}

function updateCategory($cat_name_en, $cat_name_vn, $description_en, $description_vn, $cat_parent_id, $cat_id){
    $query = "update GIAAN_CATEGORY set CAT_NAME_EN = '$cat_name_en', CAT_NAME_VN = '$cat_name_vn', CAT_DESCRIPTION_VN = '$description_vn', CAT_DESCRIPTION_EN = '$description_en', CAT_PARENT_ID = $cat_parent_id
    where CAT_ID = $cat_id";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
}

function deleteCategory($cat_id){
    $query = "delete from GIAAN_CATEGORY where CAT_ID = $cat_id";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
}

//Check if category has children
function hasChild($cat_id){
	$query = "select count(CAT_ID) as rst from GIAAN_CATEGORY where CAT_PARENT_ID = '$cat_id'";
    ConnectionManager::getInstance()->setQuery($query);
	$row = mysql_fetch_array(ConnectionManager::getInstance()->query());
	if($row['rst']>0){
		return true;
	}else{
		return false;	
	}
}

function hasProduct($cat_id){
    $query = "select count(PRO_ID) as rst from GIAAN_PRODUCT where CAT_ID = $cat_id";
    ConnectionManager::getInstance()->setQuery($query);
    $row = mysql_fetch_array(ConnectionManager::getInstance()->query());
	if($row['rst']>0){
		return true;
	}else{
		return false;	
	}
}

function isCategoryExist($cat_name_en, $cat_name_vn){
    $query = "select count(CAT_ID) as rst from GIAAN_CATEGORY where CAT_NAME_EN like '%$cat_name_en%' or CAT_NAME_VN like '%$cat_name_vn%'";
    ConnectionManager::getInstance()->setQuery($query);
    $row = mysql_fetch_array(ConnectionManager::getInstance()->query());
	if($row['rst']>0){
		return true;
	}else{
		return false;	
	}
}

function countChildCategory($cat_id) {
	$query = "select count(CAT_ID) as rst from GIAAN_CATEGORY where CAT_PARENT_ID = '$cat_id'";
	ConnectionManager::getInstance()->setQuery($query);
	$row = mysql_fetch_array(ConnectionManager::getInstance()->query());
	return $row['rst'];
}

function findFirstThreeCategories(){
	$query = "SELECT CAT_NAME_VN, CAT_ID FROM GIAAN_CATEGORY LIMIT 0, 3";
	ConnectionManager::getInstance()->setQuery($query);
	return ConnectionManager::getInstance()->query();
}

function countAllCategories(){
	$query = "SELECT COUNT(*) AS NUM FROM GIAAN_CATEGORY";
	ConnectionManager::getInstance()->setQuery($query);
	return ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
}

function getParentCategoryId($catId){
	$query = "SELECT CAT_ID FROM GIAAN_CATEGORY WHERE CAT_ID = (SELECT CAT_PARENT_ID FROM GIAAN_CATEGORY WHERE CAT_ID = $catId)";
	ConnectionManager::getInstance()->setQuery($query);
	$row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
	if(is_null($row["CAT_ID"])){
		return 0;
	}else{
	return $row["CAT_ID"];
	}
}
?>